Convert mynav to Format sub-class (#472)
authorRalf Horstmann <ralf+github@ackstorm.de>
Mon, 27 Jan 2020 21:24:04 +0000 (22:24 +0100)
committertsteven4 <13596209+tsteven4@users.noreply.github.com>
Mon, 27 Jan 2020 21:24:04 +0000 (14:24 -0700)
* Convert mynav to Format sub-class

While there, switch to gpsbabel::File, QTextStream, and introduce
enum for line type.

* Switch mynav to TextStream

* Include cleanup in mynav

* Correct includes, add link to spec for mynav format

CMakeLists.txt
GPSBabel.pro
Makefile.in
mynav.cc
mynav.h [new file with mode: 0644]
vecs.h

index f2e2bb71982032b3fa1a650905325c3efd4a27b5..aea703b4b1d9c09c412dd7382f713c05c1f0f957 100644 (file)
@@ -147,6 +147,7 @@ set(HEADERS
   legacyformat.h
   magellan.h
   mapsend.h
+  mynav.h
   navilink.h
   session.h
   shapelib/shapefil.h
index 7a31a02310afed5c09310b5014670e41bbf17d65..db32960ada239a28ee11d359002dc527014a8516 100644 (file)
@@ -135,6 +135,7 @@ HEADERS =  \
        legacyformat.h \
        magellan.h \
        mapsend.h \
+       mynav.h \
        navilink.h \
        session.h \
        shapelib/shapefil.h \
index a9b85af051335c8012589990903eca5b20ee79bb..80d5ba45283574378facc095de12c6a6fd02235c 100644 (file)
@@ -831,7 +831,8 @@ mtk_logger.o: mtk_logger.cc defs.h config.h zlib/zlib.h zlib/zconf.h \
   cet.h inifile.h gbfile.h session.h src/core/datetime.h \
   src/core/optional.h gbser.h
 mynav.o: mynav.cc defs.h config.h zlib/zlib.h zlib/zconf.h cet.h \
-  inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h
+  inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h \
+  mynav.h format.h
 navicache.o: navicache.cc defs.h config.h zlib/zlib.h zlib/zconf.h cet.h \
   inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h \
   cet_util.h src/core/file.h
@@ -1008,7 +1009,7 @@ vecs.o: vecs.cc defs.h config.h zlib/zlib.h zlib/zconf.h cet.h inifile.h \
   gbfile.h session.h src/core/datetime.h src/core/optional.h vecs.h \
   format.h gpx.h src/core/file.h src/core/xmlstreamwriter.h \
   src/core/xmltag.h legacyformat.h gbversion.h src/core/logging.h xcsv.h \
-  ggv_bin.h
+  ggv_bin.h mynav.h
 vidaone.o: vidaone.cc defs.h config.h zlib/zlib.h zlib/zconf.h cet.h \
   inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h
 vitosmt.o: vitosmt.cc defs.h config.h zlib/zlib.h zlib/zconf.h cet.h \
index 7fabf9ceb45e313d964b406719bbac74cae4a0cb..7e84ca20d36ec67e8483ab4bfd7ec95ff5798f7c 100644 (file)
--- a/mynav.cc
+++ b/mynav.cc
@@ -1,8 +1,11 @@
 /*
     Handle MyNav TRC format .trc and .ftn files
 
-    Copyright (c) 2014 Ralf Horstmann <ralf@ackstorm.de>
-    Copyright (C) 2014 Robert Lipe, robertlipe+source@gpsbabel.org
+    For information on the data format see
+    http://www.mynav.it/hwdoc/dev/TRC_Format_Spec.pdf
+
+    Copyright (c) 2014-2020 Ralf Horstmann <ralf@ackstorm.de>
+    Copyright (C) 2014-2020 Robert Lipe, robertlipe+source@gpsbabel.org
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
  */
 
-#include "defs.h"
+#include <QtCore/QChar>
 #include <QtCore/QDebug>
+#include <QtCore/QIODevice>
+#include <QtCore/QString>
 #include <QtCore/QStringList>
+#include <QtCore/QtGlobal>
+
+#include <src/core/textstream.h>
+
+#include "mynav.h"
+
+/***************************************************************************
+ *              local helper functions                                     *
+ ***************************************************************************/
 
-#define MYNAME "mynav"
-
-enum field_e {
-  fld_type = 0,
-  fld_lon,
-  fld_lat,
-  fld_direction,
-  fld_speed,
-  fld_altitude,
-  fld_timestamp,
-  fld_duration,
-  fld_gps_valid,
-  fld_distance,
-  fld_ascent,
-  fld_cadence,
-  fld_heart_rate,
-  fld_id,
-  fld_total_duration,
-  fld_terminator
-};
-
-static route_head* mynav_track;
-static gbfile* fin;
-
-//***************************************************************************
-//           local helper functions
-//***************************************************************************
-static void
-mynav_rd_line(const QString& line)
+void
+MyNavFormat::read_line(const QString& line, route_head* track)
 {
   QStringList fields = line.split("|");
 
@@ -70,11 +57,11 @@ mynav_rd_line(const QString& line)
 
   // only type 1 and type 5 lines contain coordinates
   bool ok = false;
-  int val_type = fields.at(fld_type).trimmed().toInt(&ok);
+  int line_type = fields.at(fld_type).trimmed().toInt(&ok);
   if (!ok) {
     return;
   }
-  if (val_type != 1 && val_type != 5) {
+  if (line_type != line_sensors && line_type != line_gps) {
     return;
   }
 
@@ -114,59 +101,42 @@ mynav_rd_line(const QString& line)
     }
   }
 
-  track_add_wpt(mynav_track, wpt);
+  track_add_wpt(track, wpt);
 }
 
+/***************************************************************************
+ *              entry points called by gpsbabel main process               *
+ ***************************************************************************/
 
-//***************************************************************************
-//           global callbacks called by gpsbabel main process
-//***************************************************************************
-
-static void
-mynav_rd_init(const QString& fname)
+void
+MyNavFormat::rd_init(const QString& fname)
 {
-  fin = gbfopen(fname, "rb", MYNAME);
-  mynav_track = route_head_alloc();
-  track_add_head(mynav_track);
+  read_fname = fname;
 }
 
-static void
-mynav_rd_deinit()
+void
+MyNavFormat::rd_deinit()
 {
-  gbfclose(fin);
+  read_fname.clear();
 }
 
-static void
-mynav_rd()
+void
+MyNavFormat::read()
 {
-  QString buff;
+  gpsbabel::TextStream stream;
+  stream.open(read_fname, QIODevice::ReadOnly, "mynav");
+
+  route_head* track = route_head_alloc();
+  track_add_head(track);
 
-  while ((buff = gbfgetstr(fin)), !buff.isNull()) {
-    buff = buff.trimmed();
-    if ((buff.isEmpty()) || (buff[0] == '#')) {
+  QString buf;
+  while (stream.readLineInto(&buf)) {
+    buf = buf.trimmed();
+    if ((buf.isEmpty()) || buf.startsWith('#')) {
       continue;
     }
-    mynav_rd_line(buff);
+    read_line(buf, track);
   }
-}
 
-ff_vecs_t mynav_vecs = {
-  ff_type_file,
-  {
-    ff_cap_none,  // waypoints
-    ff_cap_read,  // tracks
-    ff_cap_none   // routes
-  },
-  mynav_rd_init,    // rd_init
-  nullptr,           // wr_init
-  mynav_rd_deinit,  // rd_deinit
-  nullptr,           // wr_deinit
-  mynav_rd,         // read
-  nullptr,           // write
-  nullptr,           // exit
-  nullptr,           //args
-  CET_CHARSET_ASCII, 0  //encode,fixed_encode
-  //NULL                //name dynamic/internal?
-  , NULL_POS_OPS,
-  nullptr
-};
+  stream.close();
+}
diff --git a/mynav.h b/mynav.h
new file mode 100644 (file)
index 0000000..7276fe4
--- /dev/null
+++ b/mynav.h
@@ -0,0 +1,101 @@
+/*
+    Handle MyNav TRC format .trc and .ftn files
+
+    Copyright (c) 2014-2020 Ralf Horstmann <ralf@ackstorm.de>
+    Copyright (C) 2014-2020 Robert Lipe, robertlipe+source@gpsbabel.org
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+ */
+
+#ifndef MYNAV_H_INCLUDED_
+#define MYNAV_H_INCLUDED_
+
+#include <QtCore/QString>
+#include <QtCore/QVector>
+
+#include "defs.h"
+#include "format.h"
+
+class MyNavFormat : public Format
+{
+public:
+  ff_type get_type() const override
+  {
+    return ff_type_file;
+  }
+
+  QVector<ff_cap> get_cap() const override
+  {
+    return {
+      ff_cap_none,  // waypoints
+      ff_cap_read,  // tracks
+      ff_cap_none   // routes
+    };
+  }
+
+  QString get_encode() const override
+  {
+    return CET_CHARSET_ASCII;
+  }
+
+  int get_fixed_encode() const override
+  {
+    return 0;
+  }
+
+  void rd_init(const QString& fname) override;
+  void read() override;
+  void rd_deinit() override;
+
+private:
+  enum field_e {
+    fld_type = 0,
+    fld_lon,
+    fld_lat,
+    fld_direction,
+    fld_speed,
+    fld_altitude,
+    fld_timestamp,
+    fld_duration,
+    fld_gps_valid,
+    fld_distance,
+    fld_ascent,
+    fld_cadence,
+    fld_heart_rate,
+    fld_id,
+    fld_total_duration,
+    fld_terminator
+  };
+
+  enum line_e {
+    line_header = 0,
+    line_sensors = 1,
+    line_geonote = 2,
+    line_gps = 5,
+    line_lap_pause = 7,
+    line_lap_restart = 8,
+    line_total = 9,
+    line_lap_start = 10,
+    line_lap_end = 11,
+  };
+
+  static void read_line(const QString& line, route_head* track);
+
+  QString read_fname;
+
+};
+
+#endif
diff --git a/vecs.h b/vecs.h
index 6bb20cd57e2d94735929124bd7c86368391d4a18..1b9401d53a892b4a3c344cf36518da897f8e0a09 100644 (file)
--- a/vecs.h
+++ b/vecs.h
@@ -31,6 +31,7 @@
 #include "ggv_bin.h"
 #include "gpx.h"
 #include "legacyformat.h"
+#include "mynav.h"
 
 
 #if CSVFMTS_ENABLED
@@ -173,7 +174,6 @@ extern ff_vecs_t mapbar_track_vecs;
 extern ff_vecs_t f90g_track_vecs;
 extern ff_vecs_t mapfactor_vecs;
 extern ff_vecs_t energympro_vecs;
-extern ff_vecs_t mynav_vecs;
 extern ff_vecs_t geojson_vecs;
 extern ff_vecs_t globalsat_sport_vecs;
 #endif // MAXIMAL_ENABLED
@@ -378,7 +378,7 @@ private:
   LegacyFormat f90g_track_fmt {f90g_track_vecs};
   LegacyFormat mapfactor_fmt {mapfactor_vecs};
   LegacyFormat energympro_fmt {energympro_vecs};
-  LegacyFormat mynav_fmt {mynav_vecs};
+  MyNavFormat mynav_fmt;
   LegacyFormat geojson_fmt {geojson_vecs};
   GgvBinFormat ggv_bin_fmt;
   LegacyFormat globalsat_sport_fmt {globalsat_sport_vecs};